//
//  TaskChooseViewController.swift
//  DSHCMerchant
//
//  Created by 罗德良 on 2018/4/26.
//  Copyright © 2018年 袋鼠好车. All rights reserved.
//

import UIKit
import Alamofire

protocol TaskDelegate:NSObjectProtocol{
    func postValueToVc(str:String)
}

class TaskChooseViewController: BaseViewController {

    
    weak var delegate:TaskDelegate?
    
    @IBOutlet var leftTableview: UITableView!
    @IBOutlet var rightTableview: UITableView!
    let taskCell = "taskCell"
    var services :[ServiceModel]?
    var servicesChilds :[ServiceModel]?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        configView()
        requstStoreConfigList()
    }

    func configView() -> Void {
        leftTableview.delegate = self
        leftTableview.dataSource = self
        leftTableview.separatorStyle = .none
        leftTableview.showsVerticalScrollIndicator = false
        leftTableview.register(UINib(nibName: "TaskTableViewCell", bundle: nil), forCellReuseIdentifier: taskCell)
        rightTableview.delegate = self
        rightTableview.dataSource = self
        rightTableview.separatorStyle = .none
        rightTableview.showsVerticalScrollIndicator = false
        rightTableview.register(UINib(nibName: "TaskTableViewCell", bundle: nil), forCellReuseIdentifier: taskCell)
    }
    
    //店铺配置列表
    func requstStoreConfigList() -> Void {
        
        Alamofire.request(storeConfigListUrl, method:.post, parameters: ["sid":UserModel.shareUser.data?.storeId ?? String()])
            .responseString { response in
                
                switch response.result.isSuccess {
                case true:
                    
                    var basemodel = BaseResponseOfModel<BaseArrayData<ServiceModel>>()
                    basemodel = BaseResponseOfModel.deserialize(from: response.value)!
                    
                    switch basemodel.status! {
                    case true:
                        
                self.services = basemodel.data?.data
                let model = self.services![0]
                model.isSelected = true
                var Childs = [ServiceModel]()
                if (model.son?.count)! > 0 {
                    for serviceChild in model.son! {
                        Childs.append(serviceChild)
                        
                    }
                }
                self.servicesChilds = Childs
                self.leftTableview.reloadData()
                self.rightTableview.reloadData()
                print("数据请求成功")
                        
                    case false:
                         MBProgressHUD.alertShow(in: self.view, message:basemodel.msg, hidenAfter: 0.8, mode: MBProgressHUDMode.text)
                        
                    }
                case false:
                    print(response.result.error!)
                }
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

extension TaskChooseViewController: UITableViewDelegate,UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if tableView == leftTableview {
            return self.services?.count ?? 0
        }else {
            return self.servicesChilds?.count ?? 0
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: taskCell, for: indexPath) as! TaskTableViewCell
        cell.selectionStyle = .none
        if tableView == leftTableview {
            let model = self.services![indexPath.item]
            cell.titleLabel.text = model.name
            cell.titleLabel.font = UIFont.systemFont(ofSize: 15)
            if model.isSelected {
                cell.titleLabel?.textColor = BaseColor.NavColor
            }else {
                cell.titleLabel?.textColor = BaseColor.GrayColor
            }
            
        }else {
           let model = self.servicesChilds![indexPath.item]
            cell.titleLabel.text = model.name
            cell.titleLabel.font = UIFont.systemFont(ofSize: 13)
        }
    
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return ScaleHeight(40)
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        if tableView == leftTableview {
            let model = self.services![indexPath.item]
            
            for smodel in self.services! {
                smodel.isSelected = false
            }
            model.isSelected = true
            leftTableview.reloadData()
            if (model.son?.count)! > 0 {
                self.servicesChilds?.removeAll()
                for serviceChild in model.son! {
                    self.servicesChilds?.append(serviceChild)
                    
                }
                rightTableview.reloadData()
            }

        }else {
            let model = self.servicesChilds![indexPath.item]
            delegate?.postValueToVc(str: model.name)
            self.navigationController?.popViewController(animated: true)
        }
        
//        let model = self.orders![indexPath.item]
//        let orderDetailsVC = OrderDetailsViewController()
//        orderDetailsVC.title = "订单详情";
//        orderDetailsVC.ordermodel = model
//        self.navigationController?.pushViewController(orderDetailsVC, animated: true)
    }
    
}


